//
//  话题or知识列表
//  yilutongxing
//
//  Created by ios-23 on 17/4/10.
//  Copyright © 2017年 YZE. All rights reserved.
//

#import "Type_topicTableViewController.h"
#import "TypeTpoicTableViewCell.h"
#import "TopicDetailViewController.h"
#import "KnowledgeDetailViewController.h"
#import "YZECommunityTableViewCell2.h"

#define TypeTopicURL @"/api/topics?graftingid="
#define TypeKnowledgeURL @"/api/transplant-knowledges?graftingid="

#define BUNDLE_ID_CELL1 @"TypeTpoicTableViewCell"
#define BUNDLE_ID_CELL2 @"YZECommunityTableViewCell2"

@interface Type_topicTableViewController ()
@property(nonatomic,strong)NSMutableArray *data;
@property(nonatomic,assign)NSInteger ID;
@end

@implementation Type_topicTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    switch (self.typeID) {
        case 0:
            self.title = @"心脏移植";
            self.ID = 1;
            break;
        case 1:
            self.title = @"肝移植";
            self.ID = 2;
            break;
        case 2:
            self.title = @"肺移植";
            self.ID = 3;
            break;
        case 3:
            self.title = @"肾移植";
            self.ID = 4;
            break;
        case 4:
            self.title = @"小肠移植";
            self.ID = 5;
            break;
        case 5:
            self.title = @"胰腺移植";
            self.ID = 6;
            break;
        case 6:
            self.title = @"其他移植";
            self.ID = 101 ;
            break;
    }
    if (self.type) {
        [self creatViewWithName:@"全部话题"];

        [[YZENetwork sharedInstace]getRequestWithUrl:[NSString stringWithFormat:@"%@%@%ld",BASE,TypeTopicURL,(long)self.ID] parameters:nil delegate:self requestTag:1 successBlock:^(id responseObject) {
            self.data=[YZEModel mj_objectArrayWithKeyValuesArrayAdd:responseObject];
            [self.tableView reloadData];
        }];
    }else{
        [self creatViewWithName:@"全部移植知识"];

        [[YZENetwork sharedInstace]getRequestWithUrl:[NSString stringWithFormat:@"%@%@%ld",BASE,TypeKnowledgeURL,(long)self.ID] parameters:nil delegate:self requestTag:1 successBlock:^(id responseObject) {
            self.data=[YZEModel mj_objectArrayWithKeyValuesArrayAdd:responseObject];
            [self.tableView reloadData];
        }];
    }
    [self.tableView registerNib:[UINib nibWithNibName:BUNDLE_ID_CELL1 bundle:[NSBundle mainBundle]]
         forCellReuseIdentifier:BUNDLE_ID_CELL1];
    [self.tableView registerNib:[UINib nibWithNibName:BUNDLE_ID_CELL2 bundle:[NSBundle mainBundle]]
         forCellReuseIdentifier:BUNDLE_ID_CELL2];
}

-(void)creatViewWithName:(NSString *)string{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 55)];
    view.backgroundColor = [UIColor whiteColor];
    UIView *v1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 15)];
    v1.backgroundColor = [UIColor colorWithRed:238/255.0 green:238/255.0 blue:238/255.0 alpha:1];
    UIView *v2 = [[UIView alloc]initWithFrame:CGRectMake(10, 20, 5, 23)];
    v2.backgroundColor = [UIColor colorWithRed:55/255.0 green:147/255.0 blue:235/255.0 alpha:1];;
    v2.layer.cornerRadius = 5;
    UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(20, 20, 200, 20)];
    lable.text = string;
    [view addSubview:v1];
    [view addSubview:v2];
    [view addSubview:lable];
    self.tableView.tableHeaderView = view;
    
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navigationbarBackGroundImage"] forBarMetrics:UIBarMetricsDefault];
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = YES;
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.data.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell;
    YZEModel *model=self.data[indexPath.row];
    if (!self.type) {
        TypeTpoicTableViewCell *cell_t = [tableView dequeueReusableCellWithIdentifier:BUNDLE_ID_CELL1 forIndexPath:indexPath];
        cell_t.titleLable.text = model.name;
        cell_t.dateLable.text = model.createdDate;
        
        cell = cell_t;
    }
    else {
        YZECommunityTableViewCell2 *cell_t = [tableView dequeueReusableCellWithIdentifier:BUNDLE_ID_CELL2 forIndexPath:indexPath];
        [cell_t showData:model superWidth:tableView.frame.size.width];
        
        cell = cell_t;
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat h = 90;
    if (self.type) {
        YZECommunityTableViewCell2 *cell = [tableView dequeueReusableCellWithIdentifier:@"YZECommunityTableViewCell2"];
        h = [cell cellHeight:self.data[indexPath.row] superWidth:tableView.width];
    }
    return h;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.1;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.type) {
        TopicDetailViewController *detail=[TopicDetailViewController new];
        UINavigationController *navi=[[UINavigationController alloc]initWithRootViewController:detail];
        detail.topicDetail=self.data[indexPath.row];
        
        [self presentViewController:navi animated:YES completion:nil];
    }else{
        KnowledgeDetailViewController *detail=[KnowledgeDetailViewController new];
        UINavigationController *navi=[[UINavigationController alloc]initWithRootViewController:detail];
        YZEModel *model=self.data[indexPath.row];
        [detail getWebID:model.ID];
        [self presentViewController:navi animated:YES completion:nil];
    }
    
    
}

@end
